﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProjectStore30032013.PRESENTATION;
using ProjectStore30032013.DAL;

namespace ProjectStore30032013.PRESENTATION
{
    public partial class frmManageProduct : Form
    {
        public frmManageProduct()
        {
            InitializeComponent();
        }

        private void frmManageProduct_Load(object sender, EventArgs e)
        {

            load();
        }

        public void load()
        {
            DataCSQLDataContext m_dataCt = new DataCSQLDataContext(new Controler().ConnectionString);
            var k = from d in m_dataCt.Products
                    select new { d.ProductName, d.ProductID, d.RateVAT, d.Description, d.Price};
            dtgProduct.DataSource = k;            
        }

        
        private void btnProductAdd_Click(object sender, EventArgs e)
        {
            cmbUnitPrice a = new cmbUnitPrice();
            a.ShowDialog();
            load();
           
        }


        private void btnProductRepair_Click(object sender, EventArgs e)
        {

            string v_id = dtgProduct.CurrentCell.Value.ToString();
            cmbUnitPrice edit = new cmbUnitPrice();
            edit.displayIsEdit(v_id);
            edit.ShowDialog();
            load();
        }
        private void btnProductDel_Click(object sender, EventArgs e)
        {
            DialogResult ds = new DialogResult();
            ds = MessageBox.Show("Are you sure want to delete?", "Delete?", MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
            if (ds == DialogResult.Yes)
            {
                DataCSQLDataContext m_dataCt = new DataCSQLDataContext(new Controler().ConnectionString);
                string v_id = dtgProduct.CurrentCell.Value.ToString();
                Product v_product = (from d in m_dataCt.Products
                                     where d.ProductID == v_id
                                     select d).Single();
                m_dataCt.Products.DeleteOnSubmit(v_product);
                m_dataCt.SubmitChanges();
                load();
            }
        }

        #region Members
        Product m_product = new Product();
        DataCSQLDataContext v_dtct_product = new DataCSQLDataContext(new Controler().ConnectionString);
        #endregion

        #region Data Structures
        enum e_form_mode
        {
            THEM_SAN_PHAM = 1,
            SUA_SAN_PHAM = 2,
            XEM_SAN_PHAM = 3
        }
        #endregion

        #region Private Methods
        //Gán thông tin từ form vào object
        private void form_to_obj()
        {
            
            

        }

        //Gán thông tin từ object lên form
        private void obj_to_fomr()
        {

        }

        private void insert_obj_to_database()
        {

        }

        private void update_obj_to_database()
        {

        }

        private void lam_lai_obj_to_database()
        {

        }

        private void set_form_load()
        {

        }
        #endregion

        #region Events
        private void set_form_event()
        {
            //btn_thoat.Click += new EventHandler(btn_thoat_Click);
            //this.KeyDown += new KeyEventHandler(frm_cap_nhat_hang_ban_KeyDown);
            //btn_thêm.Click += new EventHandler(btn_thêm_Click);
        }

        void btn_thêm_Click(object sender, EventArgs e)
        {
            try
            {
                insert_obj_to_database();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            MessageBox.Show("Đã nhập thông tin thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        void frm_cap_nhat_hang_ban_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                switch (e.KeyCode)
                {
                    case Keys.Escape:
                        this.Close();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void btn_thoat_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void frm_cap_nhat_hang_ban_Load(object sender, EventArgs e)
        {
            set_form_load();
        }
        #endregion

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void dtgProduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
